//
//  UIImage+Additions.m
//  Untitled
//

#import "UIImage+Additions.h"

@implementation UIImage (Additions)

+ (UIImage *)newImageFromResource:(NSString *)filename {
	NSString *imageFile = [[NSString alloc]
						   initWithFormat:@"%@/%@",
						   [[NSBundle mainBundle] resourcePath],
						   filename];
	UIImage *image = [[UIImage alloc]
					  initWithContentsOfFile:imageFile];
	[imageFile release];
	return [image autorelease];
}

/*
- (void)viewDidLoad {
	
	float level = [[UIDevice currentDevice] batteryLevel];
	bool batteryState = [[UIDevice currentDevice] batteryState];
	
	if (batteryState && level<0.1) {
		NSLog(@"battery almost empty! (%f)", level);
	}
}
 */

- (void)test1 {
	NSString *myString = [[NSString alloc] initWithString:@"some value"];
	myString = [NSString stringWithString:@"some value"];
	
	UIView *myView = [[UIView alloc] init];
	myView.backgroundColor = [UIColor redColor];
	UIColor *bgColor = myView.backgroundColor;
	
	NSObject *myObject = [[NSObject alloc] init];
	
	UILabel *myLabel = [[UILabel alloc] init];
	myLabel.text = @"some text";
	[myView addSubview:myLabel];
	[myLabel release];	

	UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
}

- (NSString *)makeUserName
{
	NSString *name = [[NSString alloc] initWithString:@"new name"];
	return [name autorelease];
}

- (void)testString
{
	NSString *myString = @"some value";
	for (int i=0; i<9999; ++i) {
		NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
		
		NSString *myString2 = [myString stringByAppendingString:
							   [NSString stringWithFormat:@"%d", myString, i]];
		
		[pool release];
	}
	
	NSString *string1 = @"one";
	NSString *string2 = [string1 stringByAppendingString:@" two"];
	NSString *someObject = @"some";
	
	myString = @"some string";
	myString = [NSString stringWithFormat:@"object = %@", someObject];
	myString = @"12345";
	double doubleString = [myString floatValue];
	int intString = [myString intValue];
	int stringLength = [myString length];
	BOOL areEqual = [string1 isEqualToString:string2];
	areEqual = ([string1 caseInsensitiveCompare:string2] == NSOrderedSame);
	
	myString = @"abcdef";
	NSString *upper = [myString uppercaseString];
	NSString *lower = [myString lowercaseString];
	
	myString = @" one two three ";
	NSString *trimmed = [myString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
	
	NSString *numberString = @"one two three";
	NSString *aString = [numberString substringToIndex:3];
	NSRange range = NSMakeRange(4,3);
	aString = [numberString substringWithRange:range];
	aString = [numberString substringFromIndex:8];
	NSArray *arr = [numberString componentsSeparatedByString:@" "];
	aString = [numberString stringByReplacingOccurrencesOfString:@"three" withString:@"four"];
	NSRange foundRange = [numberString rangeOfString:@"two"];
	BOOL found = ([numberString rangeOfString:@"two"].location != NSNotFound);
	
	NSString *fileContents = [NSString stringWithContentsOfFile:@"myfile.txt"];
	NSString *fileName = @"myfile.txt";
	NSString *fileExtension = [fileName pathExtension];
	
	NSURL *url = [NSURL URLWithString:@"http://google.com"];
	NSString *pageContents = [NSString stringWithContentsOfURL:url];
}

- (void)testDate {
	NSDate *myDate = [NSDate date];
	
	NSTimeInterval secondsPerDay = 24*60*60;
	NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];

	secondsPerDay = 24*60*60;
	NSDate *now = [NSDate date];
	NSDate *yesterday = [now addTimeInterval:-secondsPerDay];

	NSDate *date1 = [NSDate date];
	NSDate *date2 = [NSDate date];
	BOOL sameDate = [date1 isEqualToDate:date2];
	NSDate *earlierDate = [date1 earlierDate:date2];
	NSDate *laterDate = [date1 laterDate:date2];
	
	NSTimeInterval secondsBetweenDates = [date2 timeIntervalSinceDate:date1];
	
	NSTimeInterval secondsUntilTomorrow = [tomorrow timeIntervalSinceNow];
	
	NSDateComponents *comp = [[NSDateComponents alloc] init];
	[comp setMonth:06];
	[comp setDay:01];
	[comp setYear:2010];
	NSCalendar *myCal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
	myDate = [myCal dateFromComponents:comp];
	
	unsigned units = NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit;
	now = [NSDate date];
	myCal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
	comp = [myCal components:units fromDate:now];
	NSInteger month = [comp month];
	NSInteger day = [comp day];
	NSInteger year = [comp year];
	
	now = [NSDate date];
	comp = [[NSDateComponents alloc] init];
	[comp setDay:01];
	myCal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
	tomorrow = [myCal dateByAddingComponents:comp toDate:now options:0];
	
	now = [NSDate date];
	NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
	[formatter setDateStyle:NSDateFormatterMediumStyle];
	NSString *friendlyDate = [formatter stringFromDate:now];
	
	now = [NSDate date];
	formatter = [[NSDateFormatter alloc] init];
	[formatter setTimeStyle:NSDateFormatterMediumStyle];
	NSString *friendlyTime = [formatter stringFromDate:now];
	
	now = [NSDate date];
	formatter = [[NSDateFormatter alloc] init];
	[formatter setDateFormat:@"yyyy-mm-dd"];
	friendlyDate = [formatter stringFromDate:now];
}

- (void)testArray {
	
	NSString *string1 = @"one";
	NSString *string2 = @"two";
	NSString *string3 = @"three";
	NSArray *myArray = [NSArray arrayWithObjects:string1, string2, string3, nil];

	NSArray *myArray2 = [NSArray arrayWithArray:myArray];
	
	NSRange range = NSMakeRange(0,2);
	NSArray *subArray = [myArray subarrayWithRange:range];
	
	int arrayLength = [myArray count];
	
	NSString *myString = [myArray objectAtIndex:0];
	
	myArray = [[NSArray alloc] initWithObjects:string1, string2, nil];
	BOOL isInArray = [myArray containsObject:string1];
	
	int index = [myArray indexOfObject:string1];
	
	for (NSString *obj in myArray) {
		NSLog(@"%@", obj);
	}
	
	for (NSString *obj in [myArray reverseObjectEnumerator]) {
		NSLog(@"%@", obj);
	}
	
	[myArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}

- (void)testMutableArray {
	NSString *string1 = @"one";
	NSString *string2 = @"two";
	NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:string1, string2, nil];
	NSString *string3 = @"three";
	[myArray addObject:string3];
	
	[myArray insertObject:string3 atIndex:0];
	
	[myArray replaceObjectAtIndex:0 withObject:string2];
	
	[myArray removeObject:string3];
	
	[myArray removeObjectAtIndex:0];
	
	NSRange range = NSMakeRange(0,2);
	[myArray removeObjectsInRange:range];
	
	[myArray removeAllObjects];
}

- (void)testDictionary {
	
	NSArray *arr1 = [NSArray arrayWithObjects:@"iPhone", @"iPod", nil];
	NSArray *arr2 = [NSArray arrayWithObjects:@"iMac", @"Mac Pro", @"Macbook", @"Macbook Pro", nil];
	NSDictionary *myDict = [[NSDictionary alloc]
							dictionaryWithObjectsAndKeys:arr1, @"mobile", arr2, @"computers", nil];
	
	int dictSize = [myDict count];
	
	NSArray *mobile = [myDict objectForKey:@"mobile"];
	
	NSArray *keys = [myDict allKeysForObject:arr1];
	
	NSArray *values = [myDict allValues];
	
	for (id key in myDict) {
		NSLog(@"key: %@, value: %@", key, [myDict objectForKey:key]);
	}
	
	NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"dict.txt"];
	BOOL success = [myDict writeToFile:filePath atomically:YES];
	
	NSDictionary *myDict2 = [NSDictionary dictionaryWithContentsOfFile:filePath];
	
	arr1 = [NSArray arrayWithObjects:@"iPhone", @"iPod", nil];
	arr2 = [NSArray arrayWithObjects:@"iMac", @"Mac Pro", @"Macbook", @"Macbook Pro", nil];
	NSMutableDictionary *myMDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:arr1, @"mobile", arr2, @"computers", nil];
	NSString *string1 = @"AppleTV";
	[myMDict setObject:string1 forKey:@"media"];
	
	NSString *string2 = @"airport express";
	[myMDict setObject:string2 forKey:@"media"];
	
	[myMDict removeObjectForKey:@"media"];
	
	NSArray *keyArray = [NSArray arrayWithObjects:@"mobile", @"computers", nil];
	[myMDict removeObjectsForKeys:keyArray];
	
	[myMDict removeAllObjects];
}

#define MY_NOTIFICATION @"MY_NOTIFICATION"

- (void)testNotification {
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(doSomething:)
												 name:@"myNotification" object:nil];

	NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:@"value", @"key", nil];
	[[NSNotificationCenter defaultCenter] postNotificationName:MY_NOTIFICATION object:myDict];
}

- (void)doSomething:(NSNotification *)aNote {
	NSDictionary *myDict = [aNote object];
	NSLog(@"%@", myDict);
}

- (void)viewDidLoad {
	
	[super viewDidLoad];
	
	NSString *myString = @"some value";
	NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys:myString, @"firstObject", nil];
	
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(doSomething:)
												 name:MY_NOTIFICATION
											   object:nil];
	
	[[NSNotificationCenter defaultCenter] postNotificationName:MY_NOTIFICATION
														object:myDict];
	
}

- (void)dealloc {
	
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	[super dealloc];
}

- (void)testTimer {
	NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
														target:self
													  selector:@selector(myTimerAction:)
													  userInfo:nil
													   repeats:NO];
	
	[myTimer invalidate];
	
	myTimer = [[NSTimer timerWithTimeInterval:10.0
									   target:self
									 selector:@selector(myTimerAction:)
									 userInfo:nil
									  repeats:NO] retain];
	
	[[NSRunLoop mainRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];
	
	NSTimeInterval secondsPerDay = 24*60*60;
	NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];
	myTimer = [[NSTimer alloc] initWithFireDate:tomorrow
									   interval:10.0
										 target:self
									   selector:@selector(myTimerAction:)
									   userInfo:nil repeats:YES];
}

- (void)myTimerAction:(NSTimer *)timer {
	NSLog(@"timer fired!: %@", [timer userInfo]);
}

- (void)viewDidLoad2 {
	
	NSTimeInterval secondsPerDay = 24*60*60;
	NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];
	NSTimer *myTimer = [[NSTimer alloc] initWithFireDate:tomorrow
									   interval:10.0
										 target:self
									   selector:@selector(myTimerAction:)
									   userInfo:nil repeats:NO];
	[[NSRunLoop mainRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];
	
	NSTimer *myTimer2 = [NSTimer timerWithTimeInterval:1.0
									   target:self
									 selector:@selector(myTimerAction:)
									 userInfo:nil repeats:NO];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
	return NO;
}

- (void)viewDidLoad3 {
	
	CGRect rect = CGRectMake(10,10,100,44);
	
	UITextField *myTextField = [[UITextField alloc] initWithFrame:rect];
	myTextField.delegate = self;
	
	[self.view addSubview:myTextField];
	[myTextField release];
}

- (void)buttonTap:(id)sender {
	NSLog(@"Button tapped");
}

- (void)viewDidLoad4 {
	
	CGRect rect = CGRectMake(10, 10, 100, 44);
	UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[myButton setFrame:rect];
	[myButton addTarget:self
				 action:@selector(buttonTap:)
	   forControlEvents:UIControlEventTouchUpInside];
	
	[self.view addSubview:myButton];
}

- (void)viewDidLoad5 {
	
	float level = [[UIDevice currentDevice] batteryLevel];
	bool batteryState = [[UIDevice currentDevice] batteryState];
	
	if (batteryState && level<0.1) {
		NSLog(@"battery almost empty! (%f)", level);
	}
}

@end
